package com.sca.order.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.sca.order.annotation.RequireToken;
import com.sca.order.entity.Good;
import com.sca.order.entity.api.BaseResult;
import com.sca.order.service.GoodService;
import com.sca.order.utils.GoodBloomFilter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liuxianfei
 * @since 2020/11/3 12:59
 */
@Api
@RestController
@RequestMapping("/order/v1/good")
public class GoodController {

    private final GoodService goodService;

    public GoodController(GoodService goodService) {
        this.goodService = goodService;
    }

    @GetMapping("/initBloom")
    @ApiOperation("初始化商品id")
    public BaseResult<?> initBloom() {
        List<Good> list = goodService.list();
        if (!list.isEmpty()) {
            List<Long> ids = list.stream().map(Good::getId).collect(Collectors.toList());
            GoodBloomFilter.init(ids);
        }
        return new BaseResult<>();
    }

    @GetMapping("/{goodId}")
    @ApiOperation("商品信息")
    @SentinelResource("goodInfo")
    @RequireToken
    public BaseResult<Good> info(@PathVariable Integer goodId) {
        return new BaseResult<>(goodService.getById(goodId));
    }

}
